﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DynamicComparer;
using Shuba.Model;

namespace Shuba.Web.master
{
    public partial class cdc_list : basePage
    {
        string msg = "";
        bool IsView = false, IsAdd = false, IsEdit = false, IsDelete = false, IsChecker = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtSearch.ClientID;
            this.Form.DefaultButton = btnSearch.UniqueID;

            string acl = GetUserAccess("CDC");

            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitSort();
                BindData();                

                LabelTitle.Text = GetPageTitle("CDC");
            }
        }

        private void BindData()
        {
            Shuba.Business.CDC _CDC = new Shuba.Business.CDC();
            List<CDCInfo> list;

            list = _CDC.GetList(txtSearch.Text);
            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<CDCInfo> comparer = new DynamicComparer<CDCInfo>(sort);
                list.Sort(comparer);
            }

            gvList.DataSource = list;
            gvList.DataBind();

            if (IsEdit) gvList.Columns[4].Visible = true; else gvList.Columns[4].Visible = false;

            if (gvList.Rows.Count > 0) btnSubmit.Visible = true; else btnSubmit.Visible = false;
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;
            BindData();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Shuba.Business.CDC _CDC = new Shuba.Business.CDC();

            foreach (GridViewRow row in gvList.Rows)
            {
                DropDownList ddlStatus = (DropDownList)row.FindControl("ddlStatus");
                string id = Convert.ToString(gvList.DataKeys[row.RowIndex].Value.ToString());
                _CDC.Save(id, ddlStatus.SelectedValue);
            }
            BindData();
            msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
            Alert(msg);
        }
    }
}
